package GUI.gameWin;

import game.Player;
import gamePlus.client.ClientRoundUnit;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import carddeck.Card;

import GUI.CardImages;
import GUI.gameWinOld.CardButton;
import GUI.gameWinOld.CardPanelInterface;

public class FreshCardPanelPlus extends JPanel{
	private JButton drawButton;
	private CardButton drawnButton;
	private ClientRoundUnit roundUnit;
	private boolean highlighted;

	public FreshCardPanelPlus(ClientRoundUnit roundUnit) {
		super();
		this.roundUnit=roundUnit;
		drawnButton = null;
		setBorder(new TitledBorder("0 kart"));
		((TitledBorder) getBorder()).setTitleColor(Color.WHITE);
		setBackground(new Color(0, 77, 0));
		createDrawButton();
		setPreferredSize(new Dimension(getPreferredSize().width, 133));
	}

	private void createDrawButton() {
		drawButton = new JButton();
		drawButton.setBorder(BorderFactory.createEmptyBorder());
		drawButton.setContentAreaFilled(false);
		ImageIcon img;
		img = CardImages.getInstance().getII("/carddeck/conv/resizedH/back");
		drawButton.setIcon(img);
		drawButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				drawCardActionPerformed(evt);
			}
		});
		add(drawButton);
		highlighted = false;
	}
	
	private void drawCardActionPerformed(ActionEvent evt) {
		roundUnit.drawCardActionPerformed();
	}
	
	public void setHighlight(boolean hl) {
		ImageIcon img;
		if (!hl) {
			img = CardImages.getInstance().getII("/carddeck/conv/resizedH/back");
			drawButton.setIcon(img);
		} else {
			img = CardImages.getInstance().getII("/carddeck/conv/resizedH/HL_B/back");
			drawButton.setIcon(img);
		}
	}
	
	public void refresh() {
		updateBorder();
		repaint();
		revalidate();
	}
	
	public void updateBorder() {
		int i = roundUnit.freshCardCount;
		String kart = "kart";
		if(i==1)
			kart+="a";
		if((i%10==2||i%10==3||i%10==4)&&i%100!=12&&i%100!=13&&i%100!=14)
			kart+="y";
			
		((TitledBorder) getBorder()).setTitle(i + " " + kart);
	}
	
	public void switchToAfterdraw(Player currentPlayer, Card drawnCard) {
		drawnButton = new CardButtonPlus(currentPlayer, roundUnit, drawnCard, true, true);
		removeAll();
		add(drawnButton);
		repaint();
		revalidate();
	}

	public void disableAfterdraw() {
		removeAll();
		highlighted=false;
		drawnButton = null;
		add(drawButton);
		repaint();
		revalidate();
	}
	
	public boolean isHighlighted() {
		return highlighted;
	}
}
